package com.csgc.camera.widget.dialog

import android.content.Context
import android.speech.tts.TextToSpeech
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.widget.dialog.adapter.PickerAdapter
import com.csgc.camera.widget.dialog.base.BottomDialog
import com.cysd.spsyxj.R

import com.hjq.shape.view.ShapeTextView
import java.util.Calendar

class DatePickerDialog(val context: Context) {
    var pickListener:OnPickListener?=null
    private lateinit var mDialog: BottomDialog
    private var yearView: RecyclerView?=null
    private var monthView: RecyclerView?=null
    private var dayView: RecyclerView?=null
    private var hourView: RecyclerView?=null
    private var minView: RecyclerView?=null

    private var cancelView:ImageView?=null
    private var sureView:TextView?=null

    private val yearManager: PickerLayoutManager by lazy {
        val build = PickerLayoutManager.Builder(context)
            .setMaxItem(5).build()
        build
    }
    private val monthManager: PickerLayoutManager by lazy {
        PickerLayoutManager.Builder(context).setMaxItem(5).build()
    }
    private val dayManager: PickerLayoutManager by lazy {
        PickerLayoutManager.Builder(context).setMaxItem(5)
            .build()
    }
    private val hourManager: PickerLayoutManager by lazy {
        PickerLayoutManager.Builder(context).setMaxItem(5)
            .build()
    }
    private val minManager: PickerLayoutManager by lazy {
        PickerLayoutManager.Builder(context).setMaxItem(5)
            .build()
    }

    private var yearPos = 0
    private var monthPos = 0
    private var dayPos = 0
    private var hourPos = 0
    private var minPos = 0

    private val calendar = Calendar.getInstance()
    private val curYear = Calendar.getInstance().get(Calendar.YEAR)
    private val yearAdapter: PickerAdapter by lazy {
        val list = ArrayList<Int>()
        for (i in 1901..2050){
            if (i == curYear){
                yearPos = curYear - 1901
            }
            list.add(i)
        }
        PickerAdapter(context,list,"年")
    }
    private val monthAdapter: PickerAdapter by lazy {
        val list = ArrayList<Int>()
        for (i in 1..12){
            list.add(i)
        }
        PickerAdapter(context,list,"月")
    }

    private val dayAdapter: PickerAdapter by lazy {
        val list = ArrayList<Int>()
        PickerAdapter(context,list,"日")
    }
    private val hourAdapter: PickerAdapter by lazy {
        val list = ArrayList<Int>()
        for (i in 0..23){
            list.add(i)
        }
        PickerAdapter(context,list,"时")
    }
    private val minAdapter: PickerAdapter by lazy {
        val list = ArrayList<Int>()
        for (i in 0..59){
            list.add(i)
        }
        PickerAdapter(context,list,"分")
    }



    init {
        initDialog()
    }
    private fun initDialog(){
        mDialog = BottomDialog(context).apply {
//            setDialogTitle("选择时间")
            setCustomView(R.layout.date_containt)
            val contentView = getContentView()
            yearView = contentView?.findViewById(R.id.rv_date_year)
            monthView = contentView?.findViewById(R.id.rv_date_month)
            dayView = contentView?.findViewById(R.id.rv_date_day)
            hourView = contentView?.findViewById(R.id.rv_date_hour)
            minView = contentView?.findViewById(R.id.rv_date_min)


            cancelView = contentView?.findViewById(R.id.stv_cancel)
            sureView = contentView?.findViewById(R.id.stv_sure)

            yearView?.layoutManager = yearManager
            monthView?.layoutManager = monthManager
            dayView?.layoutManager = dayManager
            hourView?.layoutManager = hourManager
            minView?.layoutManager = minManager

            yearView?.adapter = yearAdapter
            monthView?.adapter = monthAdapter
            dayView?.adapter = dayAdapter

            hourView?.adapter = hourAdapter
            minView?.adapter = minAdapter


            yearView?.scrollToPosition(yearPos)

            yearManager.setOnPickerListener(object :PickerLayoutManager.OnPickerListener{
                override fun onPicked(recyclerView: RecyclerView, position: Int) {
                    yearPos = position
                    calculateDay(yearAdapter.list[position],monthPos)
                }
            })
            monthManager.setOnPickerListener(object :PickerLayoutManager.OnPickerListener{
                override fun onPicked(recyclerView: RecyclerView, position: Int) {
                    monthPos = position
                    calculateDay(yearAdapter.list[yearPos],position)
                }
            })
            dayManager.setOnPickerListener(object :PickerLayoutManager.OnPickerListener{
                override fun onPicked(recyclerView: RecyclerView, position: Int) {
                    dayPos = position
                }
            })


            //月份设置0～11
            calculateDay(curYear,0)


            cancelView?.clickWithTrigger { mDialog.dismiss() }
            sureView?.clickWithTrigger {
                val year = yearAdapter.list[yearManager.getPickedPosition()]
                val month = monthAdapter.list[monthManager.getPickedPosition()]
                val day = dayAdapter.list[dayManager.getPickedPosition()]
                val hour = hourAdapter.list[hourManager.getPickedPosition()]
                val min = minAdapter.list[minManager.getPickedPosition()]

                pickListener?.onSelected(year, month, day,hour,min)

                mDialog.dismiss()
            }

            setCancelable(true)
        }
    }


    private fun calculateDay(year:Int,month:Int){
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month)
        val day = calendar.getActualMaximum(Calendar.DATE)
        dayAdapter.list.clear()
        for (i in 1..day){
            dayAdapter.list.add(i)
        }
        dayAdapter.notifyDataSetChanged()
        dayView?.scrollToPosition(0)
    }

    fun showDialog(){
        mDialog.show()
    }

    interface OnPickListener {

        /**
         * 选择完日期后回调
         */
        fun onSelected( year: Int, month: Int,day:Int,hour:Int,min:Int)


    }
}